package com.xingxuecn.limit.service.impl;

import com.xingxuecn.limit.common.model.Header;
import com.xingxuecn.limit.common.model.Page;
import com.xingxuecn.limit.common.model.RoleMenu;
import com.xingxuecn.limit.common.model.TreeNode;
import com.xingxuecn.limit.dao.HeaderDao;
import com.xingxuecn.limit.dao.RoleHeaderDao;
import com.xingxuecn.limit.dao.impl.HeaderDaoImpl;
import com.xingxuecn.limit.dao.impl.RoleHeaderDaoImpl;
import com.xingxuecn.limit.service.HeaderService;

import java.sql.SQLException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @author zgx at 2018/9/10 0010 下午 5:39
 */
public class HeaderServiceImpl implements HeaderService {
    private HeaderDao headerDao = new HeaderDaoImpl();
    @Override
    public List<Header> findAll() throws SQLException {
        return headerDao.findAll();
    }

    @Override
    public List<Header> search(Header header) throws SQLException {
        return headerDao.search(header);
    }

    @Override
    public List<TreeNode> headerTree() throws SQLException {
        List<Header> headerList = headerDao.findAll();
        for (int i = 0,size=headerList.size(); i < size; i++) {
            if (headerList.get(i).getState() == 0) {
                headerList.remove(i);
                size--;
                i--;
            }
        }
        //转成treeNode
        List<TreeNode> treeNodeList = new LinkedList<>();
        for (Header header : headerList) {
            TreeNode treeNode = new TreeNode(header.getId(), header.getParent_id(), header.getName(), header.getUrl(),null, null);
            treeNodeList.add(treeNode);
        }

        List<TreeNode> result = TreeNode.buildTree(treeNodeList);
        return result;
    }

}
